package com.fat.spzx.manager.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.fat.spzx.common.exception.GlobalException;
import com.fat.spzx.manager.mapper.SysMenuMapper;
import com.fat.spzx.manager.mapper.SysRoleMenuMapper;
import com.fat.spzx.manager.service.SysMenuService;
import com.fat.spzx.manager.util.MenuTree;
import com.fat.spzx.model.entity.system.SysMenu;
import com.fat.spzx.model.entity.system.SysUser;
import com.fat.spzx.model.vo.common.ResultCodeEnum;
import com.fat.spzx.model.vo.system.SysMenuVo;
import com.fat.spzx.utils.AuthContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    /**
     * 查询所有菜单
     */
    @Override
    public List<SysMenu> findNodes() {
        //1.查出全部菜单
        List<SysMenu> list = sysMenuMapper.queryAll();
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }
        //2.通过递归构建菜单树
        List<SysMenu> sysMenus = MenuTree.buildTree(list);
        if (CollectionUtil.isEmpty(sysMenus)) {
            return null;
        }
        //3.返回菜单树
        return sysMenus;
    }

    /**
     * 添加菜单
     */
    @Override
    public void save(SysMenu sysMenu) {
        //  保存到数据库
       sysMenuMapper.save(sysMenu);

        // 新添加一个菜单，那么此时就需要将该菜单所对应的父级菜单设置为半开
        updateSysRoleMenuIsHalf(sysMenu) ;
    }

    /**
     * 修改菜单
     */
    @Override
    public void updateById(SysMenu sysMenu) {
        sysMenuMapper.updateById(sysMenu);
    }

    /**
     * 删除菜单
     */
    @Override
    public void removeById(Long id) {
        //1.判断当前菜单是否有子菜单，有则不能删除
        int count = sysMenuMapper.countByParentId(id);
        if(count>0) throw new GlobalException(ResultCodeEnum.NODE_ERROR);
        //2.没有则删除
        sysMenuMapper.removeById(id);
    }

    /**
     * 根据用户查询菜单
     */
    @Override
    public List<SysMenuVo> findUserMenuList() {
        SysUser user = AuthContextUtil.getUser();
        Long id = user.getId();
        //1.查询菜单
        List<SysMenu> list = sysMenuMapper.selectListByUserId(id);
        //2.构建树形结构
        List<SysMenu> sysMenuVoList = MenuTree.buildTree(list);
        //3.返回
        return buildMenus(sysMenuVoList);
    }

    // 将List<SysMenu>对象转换成List<SysMenuVo>对象，为findUserMenuList方法调用
    private List<SysMenuVo> buildMenus(List<SysMenu> menus) {

        List<SysMenuVo> sysMenuVoList = new LinkedList<SysMenuVo>();
        for (SysMenu sysMenu : menus) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenu> children = sysMenu.getChildren();
            if (!CollectionUtil.isEmpty(children)) {
                sysMenuVo.setChildren(buildMenus(children));
            }
            sysMenuVoList.add(sysMenuVo);
        }
        return sysMenuVoList;
    }



    private void updateSysRoleMenuIsHalf(SysMenu sysMenu) {

        // 查询是否存在父节点
        SysMenu parentMenu = sysMenuMapper.selectById(sysMenu.getParentId());

        if(parentMenu != null) {

            // 将该id的菜单设置为半开
            sysRoleMenuMapper.updateSysRoleMenuIsHalf(parentMenu.getId()) ;

            // 递归调用
            updateSysRoleMenuIsHalf(parentMenu) ;

        }

    }
}

